package com.algernoon.springmail.mail.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.algernoon.springmail.mail.entity.Email;
import com.algernoon.springmail.mail.service.ISendEmailService;

@RestController
public class EmailTestController {

	private Logger logger = LoggerFactory.getLogger(EmailTestController.class);
	
	@Value("${spring.mail.username}")
	private String emailFrom;
	
	@Autowired
	private ISendEmailService sendEmailService;
	
	@PostMapping(value = "/sendEmail", consumes = {"application/json"})
	@ResponseBody
	public Object sendEmail(@RequestBody Email email) {
		
		logger.info("[sendEmail] request params {}", email);

		if (email != null) {
			email.setSender(emailFrom);
		}
		
		sendEmailService.sendEmail(email);
		
		return email;
	}
	
}
